//
//  ShieldZone.m
//  ProjectL
//
//  Created by Nguyen Tan Loc on 12/6/13.
//  Copyright (c) 2013 Nguyen Tan Loc. All rights reserved.
//

#import "ShieldZone.h"
#import "Utility.h"
#import "LevelManager.h"
#import "DefineXML.h"
#import "MainGameScene.h"
#import "ItemIcon.h"

@implementation ShieldZone

-(id) initWithUserNode:(PNode *)userNode
{
    if (self = [super init])
    {
        mUserNode = userNode;
        mShieldZone = [[CCSprite spriteWithSpriteFrameName:SFN_SHIELD_ZONE] retain];
        mElementRed = [[CCSprite spriteWithSpriteFrameName:SFN_SHIELD_ELEMENT] retain];
        mElementGreen = [[CCSprite spriteWithSpriteFrameName:SFN_SHIELD_ELEMENT] retain];
        mElementBlue = [[CCSprite spriteWithSpriteFrameName:SFN_SHIELD_ELEMENT] retain];
        
        [mElementRed setColor:CL_RED];
        [mElementGreen setColor:CL_GREEN];
        [mElementBlue setColor:CL_BLUE];
        
        mRadius = mShieldZone.contentSize.width / 2;
        mNumElement = 3;
        
        [self scheduleUpdate];
        
        CCActionTween *tween = [CCActionTween actionWithDuration:0.7 key:@"mRadius" from:0 to:mShieldZone.contentSize.width / 2];
        [self runAction:tween];
        
        [mShieldZone setOpacity:0];
        CCDelayTime *delayTime = [CCDelayTime actionWithDuration:0.7];
        CCFadeTo *fadeTo = [CCFadeTo actionWithDuration:0.3 opacity:255];
        CCSequence *sequence = [CCSequence actions:delayTime, fadeTo, nil];
        [mShieldZone runAction:sequence];
    }
    
    return self;
}

-(BOOL) destroyElement
{
    
    if ([LevelManager getInstance]._itemShieldTime > 0)
        return YES;
    
    if (mNumElement == 0)
    {
        CCLOG(@"No more element !!!");
        return NO;
    }
    
    CCSprite *spr;
    if (mNumElement == 3)
        spr = mElementRed;
    else if (mNumElement == 2)
        spr = mElementGreen;
    else
        spr = mElementBlue;
    
    [[Utility getInstance] playEffect:SOUND_BOMB_EXPLOSION];
    CCAction *action = [[Utility getInstance] getAnimWithKey:XML_ANIM_BOMB delay:0.03 loop:1];
    CCCallBlock *callBlock = [CCCallBlock actionWithBlock:^{
        [spr setVisible:NO];
    }];
    CCSequence *sequence = [CCSequence actions:(CCFiniteTimeAction *)action, callBlock, nil];
    [spr runAction:sequence];
    [spr setScale:0.4];
    
    [LevelManager getInstance]._itemShieldTime = 1;
    
    mNumElement--;
    
    if (mNumElement == 0)
    {
        CCScaleTo *scaleTo = [CCScaleTo actionWithDuration:0.3 scale:0];
        [mShieldZone runAction:scaleTo];
        ItemIcon *ii = [MainGameScene getInstance]._mainGameUI._miItemGlove;
        [ii setNumItemUse:0];
    }
    
    return YES;
}

-(void) reset
{
    
    CCLOG(@"reset");
    [mShieldZone stopAllActions];
    
    [mElementRed setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:SFN_SHIELD_ELEMENT]];
    [mElementGreen setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:SFN_SHIELD_ELEMENT]];
    [mElementBlue setDisplayFrame:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:SFN_SHIELD_ELEMENT]];
    
    [mElementRed setVisible:YES];
    [mElementGreen setVisible:YES];
    [mElementRed setVisible:YES];
    [mShieldZone setScale:1];
    
    mNumElement = 3;
}

-(void) addToBatch:(CCSpriteBatchNode *)batch
{
    [batch addChild:mShieldZone z:Z_ORDER_SHIELD_ZONE];
    [batch addChild:mElementRed z:Z_ORDER_SHIELD_ELEMENT];
    [batch addChild:mElementGreen z:Z_ORDER_SHIELD_ELEMENT];
    [batch addChild:mElementBlue z:Z_ORDER_SHIELD_ELEMENT];
    
    [mShieldZone release];
    [mElementRed release];
    [mElementGreen release];
    [mElementBlue release];
}

#define ELEMENT_SPEED   120
-(void) update:(ccTime)delta
{
    mRotation += ELEMENT_SPEED * delta;
    
    [mShieldZone setPosition:mUserNode.position];
    
    float radian = CC_DEGREES_TO_RADIANS(mRotation);
    [mElementRed setPosition:ccpAdd(ccp(mRadius * cosf(radian), mRadius * sinf(radian)), mUserNode.position)];
    
    radian = CC_DEGREES_TO_RADIANS(mRotation + 120);
    [mElementGreen setPosition:ccpAdd(ccp(mRadius * cosf(radian), mRadius * sinf(radian)), mUserNode.position)];
    
    radian = CC_DEGREES_TO_RADIANS(mRotation + 240);
    [mElementBlue setPosition:ccpAdd(ccp(mRadius * cosf(radian), mRadius * sinf(radian)), mUserNode.position)];
    
    
    [LevelManager getInstance]._itemShieldTime -= delta;
}

@end
